package cn.shopifymall.springbucks_consumer.controller;

import cn.shopifymall.springbucks_consumer.entity.Coffee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Author 孤风雪影
 * @Email gitee.com/efairy520
 * @Date 2022/8/6 22:46
 * @Version 1.0
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/coffee/count")
    public long queryCoffeeCount() {
        Long count = restTemplate.getForObject("http://127.0.0.1:8080/coffee/count", Long.class);
        if (count != null) {
            return count;
        }
        return 0;
    }

    @PostMapping("/queryCoffeeByIds")
    public List<Coffee> queryCoffeeByIds(@RequestBody List<String> ids) {
        ResponseEntity<List<Coffee>> exchange = restTemplate.exchange(
                "http://localhost:8080/coffee/getCoffeeByIds",
                HttpMethod.POST,
                new HttpEntity<>(ids),
                new ParameterizedTypeReference<List<Coffee>>() {
                });
        List<Coffee> coffeeList = exchange.getBody();
        return coffeeList;
    }
}
